// pages/coupon/index.js
const app = getApp()
var url = app.getURL();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bussiType: 1001,
    list: [],
    orderStatus: 1000
  },
  tabFun(e) {
    this.setData({
      orderStatus: e.currentTarget.dataset.index
    })
    this.getList()
  },
  getList(){
    let that = this;
    wx.request({
      url: url+'/order/app/order/auth/qureyOrderItemList',
      method: "POST",
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "bussiType": that.data.bussiType,
        "orderStatus": that.data.orderStatus
      },
      success(res) {
        console.log(res.data.data)
        that.setData({
          "list": res.data.data
        })
      }
    })
  },
  // 订单支付
  goPay(e){
    let that = this;
    let payNbr=e.currentTarget.dataset.pay
    console.log(payNbr)
    wx.request({
      url: url+'/order/app/order/auth/payOrder',
      method: "POST",
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data: payNbr,
      success(res) {
        console.log(res)
        if(res.data.code==200){
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1000
          })
          that.getList()
        }else if(res.data.code==301){
          wx.showToast({
            title: '钱包未开通',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
        },1500)
        }else if(res.data.code==412){
          wx.showToast({
            title: '订单不存在',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
        },1500)
        }else if(res.data.code==413){
          wx.showToast({
            title: '订单支付失败',
            icon: 'error',
            duration: 1000
          })
          setTimeout(function(){
            wx.redirectTo({
                url: '/pages/my/user/wallet/wallet'
            })  
        },1500)
        }else{
          wx.showToast({
            title: '支付失败',
            icon: 'error',
            duration: 1000
          })
        }
        
      }
    })
  },
  // 订单确认
  confirm(e){
    // console.log(e)
    let id = wx.getStorageSync('message').id
    let orderItemId = e.currentTarget.dataset.con
    wx.request({
      url: url+'/order/app/order/auth/confirmOrder/'+id+'/'+orderItemId,
      method: "POST",
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success(res) {
        console.log(res)
        if (res.data.code==200) {
          wx.showToast({
            title: '确认成功',
            icon: 'success',
            duration: 600
          })
        }else{
          wx.showToast({
            title: '出错啦~',
            icon: 'error',
            duration: 800
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      // 接受页面传参
      bussiType: options.type
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})